package com.itany.netclass.controller;

import com.itany.mvc.annotation.RequestMapping;
import com.itany.mvc.annotation.ResponseBody;
import com.itany.netclass.constant.ResponseCodeConstant;
import com.itany.netclass.entity.User;
import com.itany.netclass.exception.*;
import com.itany.netclass.factory.ObjectFactory;
import com.itany.netclass.service.FrontMainControllerService;
import com.itany.netclass.util.ResponseResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Author:sjy@itany.com
 * Date:21-10-20 上午10:56
 * Version:1.0-SNAPSHOT
 * Description:
 */
@RequestMapping("/showFrontMain")
public class FrontMainController {

    @RequestMapping("/findCourseType")
    public  String findCourseType(HttpServletRequest request, HttpServletResponse response){

        return "/showFrontMain.do";
    }
    @ResponseBody
    @RequestMapping("/login")
    public ResponseResult login(HttpServletRequest request, HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        String username = request.getParameter("username");
        System.out.println(username);
        String password = request.getParameter("password");
        System.out.println(password);
        FrontMainControllerService frontMainControllerService= (FrontMainControllerService) ObjectFactory.getObject("FrontMainControllerService");
        try {
            User login = frontMainControllerService.login(username, password);
            request.getSession().setAttribute("floginMrg",null);
            System.out.println(login);
            request.getSession().setAttribute("FloginUser",login);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            result.setReturnObject(login);
        } catch (NameOrPasswordException e) {
            e.printStackTrace();
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            result.setMessage(e.getMessage());
        } catch (ServiceException e) {
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/exit")
    public  String exit(HttpServletRequest request,HttpServletResponse response){
        request.getSession().setAttribute("FloginUser",null);
        return "/showFrontMain.do";
    }
    @ResponseBody
    @RequestMapping("/jianchaUsername")
    public  ResponseResult jianchaUsername(HttpServletRequest request,HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        String username = request.getParameter("username");
        FrontMainControllerService frontMainControllerService= (FrontMainControllerService) ObjectFactory.getObject("FrontMainControllerService");
        try {
            frontMainControllerService.jianchaUsername(username);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (TpyeException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (EmailExistException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/jianchaPassword")
    public  ResponseResult jianchaPassword(HttpServletRequest request,HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        String password = request.getParameter("password");
        System.out.println("密码");
        FrontMainControllerService frontMainControllerService= (FrontMainControllerService) ObjectFactory.getObject("FrontMainControllerService");
        try {
            frontMainControllerService.jianchaPassword(password);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (TpyeException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/jianchaRePassword")
    public  ResponseResult jianchaRePassword(HttpServletRequest request,HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        String password = request.getParameter("password");
        String rePassword = request.getParameter("rePassword");
        System.out.println("确认密码");
        FrontMainControllerService frontMainControllerService= (FrontMainControllerService) ObjectFactory.getObject("FrontMainControllerService");
        try {
            frontMainControllerService.jiancharePassword(password,rePassword);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (TpyeException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/jianchaNickname")
    public  ResponseResult jianchaNickname(HttpServletRequest request,HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        String nickname = request.getParameter("nickname");
        FrontMainControllerService frontMainControllerService= (FrontMainControllerService) ObjectFactory.getObject("FrontMainControllerService");
        try {
            frontMainControllerService.jiachaNickname(nickname);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/jianchaEmail")
    public  ResponseResult jianchaEmail(HttpServletRequest request,HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        String email = request.getParameter("email");
        FrontMainControllerService frontMainControllerService= (FrontMainControllerService) ObjectFactory.getObject("FrontMainControllerService");
        try {
            frontMainControllerService.jianchaEmil(email);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (TpyeException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (EmailExistException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/addUser")
    public  ResponseResult addUser(HttpServletRequest request,HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        String username = request.getParameter("username");
        String password=request.getParameter("password");
        String nickname=request.getParameter("nickname");
        String email = request.getParameter("email");
        FrontMainControllerService frontMainControllerService= (FrontMainControllerService) ObjectFactory.getObject("FrontMainControllerService");
        try {
            frontMainControllerService.addUser(username,password,nickname,email);
            result.setMessage("注册成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
}
